package com.contractpage.ec;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.contractpage.common.persistence.Page;
import com.contractpage.ec.entity.Appoint;
import com.contractpage.ec.entity.ContractInbound;
import com.contractpage.ec.service.AppointService;
import com.contractpage.ec.service.ContractDetailService;
import com.contractpage.ec.service.ContractInboundService;
import com.contractpage.util.StringUtils;
import com.richgo.thrsys.service.reserve.ThrsysReserveService;

@Controller
@RequestMapping(value = "contractInbound")
public class ContractInboundController extends BaseController{

	@Autowired
	private ContractInboundService contractInboundService;

	@Autowired
	private ContractDetailService contractDetailService;
	
	@Autowired
	private AppointService appointService;
	
	
	@RequestMapping(value = "findQs")
	@ResponseBody
	public Object findQs(ContractInbound entity, HttpServletRequest request, HttpServletResponse response, Model model){
		List<ContractInbound> list = contractInboundService.findQs(entity);
		for(ContractInbound inbound : list){
			Appoint app = new Appoint();
			app.setPrinter(inbound.getPrinter());
			app.setProjectcode(inbound.getProjectcode());
			inbound.setNeed(appointService.findCount(app));
		}
		Page<ContractInbound> page = new Page(request,response);
		page.setList(list);
		return  page;
	}
	
	@RequestMapping(value = "findList")
	@ResponseBody
	public Object findList(ContractInbound entity, HttpServletRequest request, HttpServletResponse response, Model model){
		Page<ContractInbound> page = new Page<ContractInbound>(request,response);
		page = contractInboundService.findList(page, entity);
		for(ContractInbound inbound : page.getList()){
			Appoint app = new Appoint();
			app.setPrinter(inbound.getPrinter());
			app.setProjectcode(inbound.getProjectcode());
			inbound.setNeed(appointService.findCount(app));
		}
		return  page;
	}
	@RequestMapping(value = "save")
	@ResponseBody
	public Object save(ContractInbound entity, HttpServletRequest request, HttpServletResponse response, Model model){
		setUser(entity);
		int i = StringUtils.isEmpty(entity.getId())?contractInboundService.insert(entity):contractInboundService.update(entity);
		return i;
	}
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(ContractInbound entity, HttpServletRequest request, HttpServletResponse response, Model model){
		return contractInboundService.delete(entity);
	}
	
	
	/**
	 * 绑定分发
	 * @param entity
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "saveDistributes")
	@ResponseBody
	public Object saveDistributes(ContractInbound entity, HttpServletRequest request, HttpServletResponse response){
		
		/*for(TgetReserveMsgByProjectNameVO appoint : entity.getAppoints()){
			ThrsysEmp emp = thrsysIncrementService.getPsndocAllInfo(appoint.getBankAcco());
			appoint.setBankAcco(emp.getDeptCode()); 
			emp.getDeptName();
		}*/
		//ThrsysEmp emp = thrsysIncrementService.getPsndocAllInfo(entity.getAppoints());
		setUser(entity);
		return contractInboundService.saveDistributes(entity);
	}
	
	
	/**
	 * 合同分发页面
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findDistributes")
	@ResponseBody
	public Object findDistributes(ContractInbound entity, HttpServletRequest request, HttpServletResponse response, Model model){
		Page<ContractInbound> page = new Page<ContractInbound>(request,response);
		page = contractInboundService.findDistributes(page, entity);
		Appoint app = new Appoint();
		for(ContractInbound inbound : page.getList()){
			app.setStatus("0");
			app.setPrinter(inbound.getPrinter());
			app.setProjectcode(inbound.getProjectcode());
			//List<Appoint> list = appointService.find(app);
			//待分发
			inbound.setNeedAppoints(appointService.findCount(app));
			//inbound.setApps(list);
			app.setStatus("1");
			//已经分发
			inbound.setAppointeds(appointService.findCount(app));
		}
		return page;
	}
	/**
	 * 合同配送页面
	 * @param entity
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findContractAppoint")
	@ResponseBody
	public Object findContractAppoint(ContractInbound entity, HttpServletRequest request, HttpServletResponse response, Model model){
		Page<ContractInbound> page = new Page<ContractInbound>(request,response);
		page = contractInboundService.findContractAppoint(page, entity);
		for(ContractInbound inbound : page.getList()){
			ContractInbound bound = new ContractInbound();
			bound.setProjectcode(inbound.getProjectcode());
			bound.setPrinter(inbound.getPrinter());
			//bound.setDepartment(inbound.getDepartment());
			//bound.setPrinter(inbound.getPrinter());
			if(StringUtils.isEmpty(inbound.getPrinter())){
				continue;
			}
			bound = contractInboundService.get(bound);
			if(bound == null||StringUtils.isEmpty(bound.getId())){
				continue;
			}
			inbound.setProjectname(bound.getProjectname());
			inbound.setDispatchnumber(bound.getDispatchnumber());
			inbound.setTotal(bound.getTotal());
			inbound.setFilein(bound.getFilein());
			inbound.setCancel(bound.getCancel());
			inbound.setId(bound.getId());
		}
		return page;
	}
}